layui.use(['layer','form','table','element','upload'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,upload = layui.upload
        ,layedit = layui.layedit
        ,element = layui.element;

    //监听工具条
    table.on('tool(dataTable)',function (obj) {
        var data = obj.data;
        if(obj.event == 'detail'){
            //查看项目详情
            var url = "/admin/app/edit/"+data.id;
            $(this).attr("data-url",url);
            parent.addTab($(this))
        }else if(obj.event == 'del'){
            //删除项目

            layer.confirm('确定删除？', function(index){
                layer.msg('删除中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

                // $.ajax({
                //     type:"POST"
                //     ,url:"/admin/project/delete"
                //     ,data:{
                //         id:data.id
                //     }
                //     ,success:function (result) {
                //         layer.msg(result.message);
                //         table.reload("data");
                //     }
                //     ,error:function () {
                //         layer.msg("系统错误");
                //     }
                //
                // });

            });
        }else {
            //编辑项目
            layer.msg("操作错误！");
        }
    });

    //初始化数据表格
    table.render({
        elem:"#data"
        ,height: 'full-150'
        ,url:"/admin/app/applist"
        ,cellMinWidth: 80
        ,cols:  [[
            {field:'id',width:60,title: 'ID', sort: true}
            ,{field:'title', title: '标题',align:'center'}
            ,{field:'mark', title: '标记',align:'center'}
            ,{field:'realDownCount', title: '下载次数',align:'center'}
            ,{field:'',title: '上下架',toolbar: '#barDemo2',align:'center'}
            ,{field:'',title: '类型',align:'center',templet: function(d){
                if(d.type == 1){
                    return '<span>APP</span>'
                }else if(d.type == 2){
                    return '<span>素材</span>'
                }
                }}
            ,{field:'',title: '操作',toolbar: '#barDemo',align:'center'}
        ]]
        ,page:true
    });

    //监听指定开关
    form.on('switch(switch)', function(data){
        var id =  data.othis.parents('tr').find("td :first").text();
        $.ajax({
            type:"POST"
            ,url:"/admin/app/offline"
            ,data:{
                id:id,
                offline:this.checked?0:1
            }
            ,success:function (result) {
                layer.msg(result.message);
            }
            ,error:function () {
                layer.msg("系统错误");
            }
        });
    });

    //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                title:$("#title").val(),
                type:$("#type").val()
            }
        })
    });

});

function toAdd() {
    window.open("/admin/app/addView", "_blank");
}
